{%- if config.google %}
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '{{config.google}}']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    if (location.port) {
       return;
    }
    ga.src = 'https://ssl.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
{%- endif %}

{%- set enginefiles = engines %}
{%- set pkg = config.package %}

{%- if enginefiles|length %}

{%- for file in enginefiles %}
<script src="{{content_url('')}}/sea-modules/{{file}}"></script>
{%- endfor %}

{%- else %}

<script src="{{config.assets_host}}/??
seajs/seajs/2.1.1/sea.js,
seajs/seajs-combo/1.0.0/seajs-combo.js,
seajs/seajs-debug/1.0.0/seajs-debug.js,
seajs/seajs-style/1.0.0/seajs-style.js,
seajs/seajs-health/0.1.0/seajs-health.js,
jquery/jquery/1.7.2/jquery.js"></script>

{%- if pkg|is_plugin_text or pkg|is_runtime_handlebars %}
<script src="{{config.assets_host}}/seajs/seajs-text/1.0.2/seajs-text.js"></script>
{%- endif %}

{%- endif %}

{%- set src = src_files %}
{%- set dist = dist_files %}
<script>
  (function() {

  var query = location.search.substring(1).split('&');
  var srcdir = 'src';

  if (contain(query, 'dist')) {
    srcdir = 'dist';
    {% for file in dist.js %}
      seajs.use("{{content_url('')}}dist/{{file}}");
    {%- endfor %}
  } else if (contain(query, 'cov')) {
    srcdir = 'src-cov';
  }

  seajs.config({
    alias: {
      "$": 'jquery/jquery/1.7.2/jquery',
      "jquery": 'jquery/jquery/1.7.2/jquery',
      "jquery-debug": 'jquery/jquery/1.7.2/jquery-debug',
      expect: "gallery/expect/0.2.0/expect",
      puerh: "popomore/puerh/0.1.0/puerh",
      handlebars: "gallery/handlebars/1.0.2/handlebars",
      sinon: "gallery/sinon/1.6.0/sinon"
    },
    map: [function(uri) {
      // resolve id
      var m = uri.match(/{{pkg.family}}\/{{pkg.name}}\/{{pkg.version}}\/(.*)/);
      if (m) {
        return resolve(m[1]);
      }
      return uri;
    }, function(uri) {
      var m = uri.match(/([A-Za-z\-]+)\/[A-Za-z\-]+\/[\.0-9]+\/.+/);
      if (m && !/seajs|jquery|gallery|arale|alice/.test(m[1])) {
        return 'http://yuan.alipay.im/assets/' + m[0];
      }
      return uri;
    }],
    comboExcludes: {test: function(uri) {
      return uri.indexOf('{{config.assets_host}}') === -1;
    }}
  });

  function resolve(file) {
    if (contain(query, 'debug')) {
      if (file.indexOf('-debug') === -1) {
        file = file.replace(/\.(\w+)$/, '-debug.$1');
      }
    }
    return seajs.resolve("{{content_url('')}}" + srcdir + "/" + file + '#');
  }

  function contain(arr, value) {
    for (var i in arr) {
      if (arr[i] === value) {
        return true;
      }
    }
    return false;
  }

  {%- if pkg.spm -%}

    {%- if pkg.spm.alias %}
      seajs.config({ alias: {{pkg.spm.alias|clean_alias|json_encode}}});
    {%- endif %}
    seajs.use({{pkg.spm.alias|css_alias|json_encode}});

    {%- if pkg.spm.devAlias %}
      seajs.config({ alias: {{pkg.spm.devAlias|clean_alias|json_encode}}});
    {%- endif %}

    seajs.config({alias: {
      "$-debug": 'jquery/jquery/1.7.2/jquery-debug'
      {%- for value in src.alias %}
        ,"{{loop.key}}": resolve("{{value}}")
      {%- endfor %}
    }});

    // 加载依赖的 css 文件
    var cssArray = [];
    {%- for value in pkg.spm.alias %}
    cssArray.push('{{value}}');
    {%- endfor %}
    for (var i=0; i<cssArray.length; i++) {
      if (cssArray[i].indexOf('.css') < 0) {
        cssArray.splice(i, 1);
      }
    }
    seajs.use(cssArray);

  {%- endif %}
  })();
</script>
